package com.yhy.wxzn.wcs.device.netty.encoder;

import com.yhy.wxzn.wcs.device.netty.bean.SiemensMessage;
import com.yhy.wxzn.wcs.device.netty.enumerate.DataType;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class SiemensEncoder extends MessageToByteEncoder<SiemensMessage> {
    public SiemensEncoder() {
    }

    protected void encode(ChannelHandlerContext ctx, SiemensMessage msg, ByteBuf out) throws Exception {
        int l = out.writerIndex();
        int dataAddress;
        switch(msg.getType()) {
            case Head1:
            case Head2:
                out.writeBytes(msg.getDataValue());
                break;
            case Read:
                out.writeByte(3);
                out.writeByte(0);
                out.writeShort(31);
                out.writeByte(2);
                out.writeByte(240);
                out.writeByte(128);
                out.writeByte(50);
                out.writeByte(1);
                out.writeByte(0);
                out.writeByte(0);
                out.writeByte(0);
                out.writeByte(1);
                out.writeByte(0);
                out.writeByte(14);
                out.writeByte(0);
                out.writeByte(0);
                out.writeByte(4);
                out.writeByte(1);
                out.writeByte(18);
                out.writeByte(10);
                out.writeByte(16);
                out.writeByte(msg.isByte() ? 2 : 1);
                out.writeShort(msg.getDataLength());
                out.writeShort(msg.getDbBlockNo());
                out.writeByte(msg.getFnCode().getValue());
                if (msg.getDataType() == DataType.Bit) {
                    dataAddress = msg.getDataAddress();
                } else {
                    dataAddress = msg.getDataAddress() * 8;
                }

                out.writeByte(dataAddress >> 16 & 255);
                out.writeByte(dataAddress >> 8 & 255);
                out.writeByte(dataAddress & 255);
                break;
            case Write:
                ByteBuf buf = msg.getDataValue();
                int dataLength = msg.getDataLength();
                out.writeByte(3);
                out.writeByte(0);
                out.writeShort(35 + dataLength);
                out.writeByte(2);
                out.writeByte(240);
                out.writeByte(128);
                out.writeByte(50);
                out.writeByte(1);
                out.writeByte(0);
                out.writeByte(0);
                out.writeByte(0);
                out.writeByte(1);
                out.writeByte(0);
                out.writeByte(14);
                out.writeByte(0);
                out.writeByte(4 + dataLength);
                out.writeByte(5);
                out.writeByte(1);
                out.writeByte(18);
                out.writeByte(10);
                out.writeByte(16);
                out.writeByte(msg.isByte() ? 2 : 1);
                out.writeShort(dataLength);
                out.writeShort(msg.getDbBlockNo());
                out.writeByte(msg.getFnCode().getValue());
                if (msg.getDataType() == DataType.Bit) {
                    dataAddress = msg.getDataAddress();
                } else {
                    dataAddress = msg.getDataAddress() * 8;
                }

                out.writeByte(dataAddress >> 16 & 255);
                out.writeByte(dataAddress >> 8 & 255);
                out.writeByte(dataAddress & 255);
                out.writeByte(0);
                out.writeByte(msg.isByte() ? 4 : 3);
                if (msg.getDataType() == DataType.Bit) {
                    out.writeShort(1);
                } else {
                    out.writeShort(dataLength * 8);
                }

                out.writeBytes(buf);
        }

    }
}
